﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;

namespace TfsUtils.Ui.Mvvm.Ui
{
    [Serializable]
    public abstract class UiObjectBase : ICanNotifyPropertyChanged, INotifyPropertyChanging, IDataErrorInfo
    {
        [field: NonSerialized]
        public virtual event PropertyChangingEventHandler PropertyChanging;

        private void OnPropertyChanging(String propertyName)
        {
            PropertyChangingEventHandler temp = PropertyChanging;
            if (temp != null)
            {

                temp(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        [field: NonSerialized]
        public virtual event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected void RaisePropertyChanged(string propertyName)
        {
            if (String.IsNullOrEmpty(propertyName))
                throw new ArgumentNullException("propertyName");
            if (PropertyChanged == null) return;
            if (SynchronizationContext.Current != null)
                SynchronizationContext.Current.Post(delegate { OnPropertyChanged(propertyName); }, null);
            else
                UiThread.Run(() => OnPropertyChanged(propertyName));
        }

        protected void RaisePropertyChanged<T>(Expression<Func<T, object>> action)
        {
            RaisePropertyChanged(((MemberExpression)action.Body).Member.Name);
        }

        protected void RaisePropertyChanging(string propertyName)
        {
            if (String.IsNullOrEmpty(propertyName))
                throw new ArgumentNullException("propertyName");
            if (PropertyChanged == null) return;
            if (SynchronizationContext.Current != null)
                SynchronizationContext.Current.Post(delegate { OnPropertyChanging(propertyName); }, null);
            else
                UiThread.Run(() => OnPropertyChanging(propertyName));
        }

        #region Validation

        private Dictionary<String, String> _errors;

        public virtual string this[string columnName]
        {
            get
            {
                if (Errors.ContainsKey(columnName))
                {
                    return Errors[columnName];
                }
                return null;
            }
        }


        public virtual string Error { get; protected set; }

        public Dictionary<string, string> Errors
        {
            get { return _errors ?? (_errors = new Dictionary<string, string>()); }
        }

        internal virtual void ClearErrors()
        {
            var keys = Errors.Keys.ToList();
            Errors.Clear();
            foreach (string key in keys)
            {
                OnPropertyChanged(key);
            }

        }

        internal virtual void AddError(String property, String error)
        {
            if (Errors.ContainsKey(property))
            {
                Errors[property] = Errors[property] + "\n" + error;
            }
            else
            {
                Errors.Add(property, error);
            }
            OnPropertyChanged(property);
        }
        #endregion

    }
}
